var app = getApp();

Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        inited : true,
        times : 1,
        other:[],
		images : [],
        mark : [],
    },
    onLoad:function(options){
        var self = this;
        wx.setNavigationBarTitle({
            title: '',
        });
        app.com.auth(function(user){
            if( options.scene && options.scene.length > 1 ){
                self.setData({
                    code : options.scene
                },function(){
                    self.select();
                });
            }
        })
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: '',
            path: '',
        }
    },
    inputvalue(e){
        var self = this;
        self.setData({
            code : e.detail.value
        })
    },
    select(){
        var self = this;
        if( !self.data.code ) {
            app.com.toast('请输入编码');return false;
        }

        app.com.http('admin','POST',{op:'selecthexiao',code:self.data.code},0,true,'','',function(res){
            if (!res.data.errno && res.data ) {
                self.setData({
                    order : res.data.data.order,
                    params : res.data.data.params,
                    good : res.data.data.good,
                    other : res.data.data.otherarr ? res.data.data.otherarr : [],
                    hxlog : res.data.data.hxlog ? res.data.data.hxlog : {},
                    mark : res.data.data.mark ? res.data.data.mark : [],
                })
            }else{
                app.com.toast(res.data.message);
                self.setData({
                    order : null,
                    good : null,
                })
                if( res.data.errno == 2 ){
                    wx.navigateTo({
                        url : '/zofui_sales/pages/admin/login'
                    });
                }
            }
        });
        
    },
    hexiao(times){
        var self = this;
        if( !self.data.code ) {
            app.com.toast('请输入编码');return false;
        }
        var postdata = {
            op:'hexiao',
            code:self.data.code,
            times: times > 0 ? times : self.data.times,
        };
        var paytips = '';
        if(self.data.order.paytype*1 == 2){
            paytips = '此订单是到店付款订单，请确保已收到资金再'+self.data.params.hxbname+',';
        }

        app.com.confirm(paytips+'确定执行'+self.data.params.hxbname+'吗？',function(){
            app.com.http('admin','POST',postdata,0,true,'','',function(res){
                if (!res.data.errno) {
                    app.com.toast(res.data.message);
                    self.setData({
                        order : null,
                        good : null,
                        code : '',
                        hxlog : false,
                    })
                }else{
                    app.com.toast(res.data.message);
                }
            });
        })
    },
    times(e){
        this.setData({
            times : e.detail.value
        })
    },
    hexiaoother(e,type){
        var self = this;
        if( !self.data.code ) {
            app.com.toast('请输入编码');return false;
        }
        var index = e.currentTarget.dataset.index;
        var postdata = {
            op : 'hexiao',
            code : self.data.code,
            oid : self.data.other[index].orderid,
            type : 'other',
        };
        if( type == 'all' ){
            self.hxfn(postdata,index);
        }else{
            app.com.confirm('确定将订单'+postdata.oid+self.data.params.hxbname+'吗？',function(){
                self.hxfn(postdata,index);
            })
        }

    },
    hxall(){
        var self = this;
        app.com.confirm('确定将所有订单都'+self.data.params.hxbname+'完吗？',function(){
            for (var i = 0; i < self.data.other.length; i++) {
                (function(i){
                    var e = {
                        currentTarget : {
                            dataset : {
                                index : i,
                            }
                        }
                    };
                    self.hexiaoother(e,'all');
                })(i)
            }
            self.hexiao(self.data.order.hxtimes);
        })
    },
    hxfn(postdata,index){
        var self = this;
        app.com.http('admin','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno) {
                app.com.toast(res.data.message);
                self.setData({
                    ['other['+index+'].hxtimes'] : 0,
                })
            }else{
                app.com.toast( res.data.message );
            }
        });
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },
    showmark(){
        this.selectComponent('#markorder').showmark();
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    scan : function( e ){
        wx.scanCode({
            success : function(res){
                if( res.path ){
                    var path =  res.path.indexOf("/") == 0 ? res.path : '/'+res.path;
                    wx.navigateTo({
                      url: path,
                    });
                }
            }
        });
    },
})